<template>
  <div class="recommend-container">
    <van-nav-bar
      left-text="返回"
      @click-left="$router.back()"
      fixed
      title="请选择推荐人"
    />
    <div class="title-padding"></div>
    <!-- <h2 class="van-doc-demo-block__title">添加联系人</h2>
    <van-contact-card type="add" @click="onAdd" /> -->
    <h2 class="van-doc-demo-block__title">我的联系人</h2>
    <van-contact-card
      v-for="(item, index) in recommendList"
      :key="index"
      type="edit"
      :name="item.name"
      :tel="item.phoneNum"
      @click="onSelect(item)"
      class="contact-card"
    />
  </div>
</template>

<script>
/* eslint-disable */
import { Toast } from "vant";
import { buildURLWithParams, getLocal } from "@/utils";
import { recommend_page } from "@/api";
export default {
  name: "recommend-select",
  data() {
    return {
      visitorData: undefined,
      recommendList: [],
      type: null,
    };
  },
  methods: {
    onSelect(item) {
      const path = buildURLWithParams("/apply-detail", {
        inviterName: item.name,
        inviterPhone: item.phoneNum,
        email: item.email,
        type: this.type ? this.type : null,
      });
      // console.log('path', path)
      this.$router.push(path);
    },
    async fetchList() {
      this.visitorData = getLocal("visitorData");
      const res = await recommend_page({
        visitorId: this.visitorData.id,
        limit: 9999,
      });
      this.recommendList = [];
      // 保证默认联系人在第一位
      res.data.list.map((item) => {
        if (item.def == "true") {
          this.recommendList.push(item);
        }
      });
      res.data.list.map((item) => {
        if (item.def != "true") {
          this.recommendList.push(item);
        }
      });
    },
  },
  created() {
    this.fetchList();
    if (this.$route.query.type) {
      this.type = this.$route.query.type;
    }
  },
  watch: {
    $route(to, from) {
      if (to.query.type) {
        this.type = to.query.type;
      }
      if (to.path == "/recommend-select") {
        this.fetchList();
      }
    },
  },
};
</script>

<style lang="less" scoped>
.recommend-container {
  padding: 0 10px;
  background: #f5f5f5;
  height: 100vh;
  max-width: 400px;
  margin: 0 auto;
  // padding: 20px;
}
.title-padding {
  padding-top: 5vh;
}
.van-doc-demo-block__title {
  margin: 0;
  padding: 32px 16px 16px;
  color: rgba(69, 90, 100, 0.6);
  font-weight: normal;
  font-size: 14px;
  line-height: 16px;
}
.contact-card {
  margin-bottom: 2vh;
}
</style>
